package org.jflame.devAide.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.MessageFormat;
import java.util.Properties;
import java.util.ResourceBundle;

import org.jflame.commons.exception.BusinessException;
import org.jflame.commons.util.UrlHelper;
import org.jflame.devAide.App;
import org.jflame.devAide.AppContext;

/**
 * 资源文件工具类
 *
 * @author yucan.zhang
 */
public final class ResourceUtils {

    private static final String CSS_DIR = "/css/";
    private static final String IMG_DIR = "/images/";
    private static final String ASSETS_DIR = "/assets/";
    private static final String FXML_DIR = "/org/jflame/devAide/";
    private static final String BUNDLE_NAME = "lang";
    private static final String SVG_IMAGE_PROPS = "/images/svgpath.properties";

    private static Properties svgProperties = null;
    static {
        svgProperties = new Properties();
        try (InputStream stream = loadResource(SVG_IMAGE_PROPS)) {
            svgProperties.load(stream);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    /**
     * 返回资源文件的物理路径
     *
     * @param relativePath 文件相对路径
     * @return
     */
    public static String absUrl(String relativePath) {
        if (relativePath == null) {
            throw new IllegalArgumentException("relativePath not be null");
        }
        try {
            return ResourceUtils.class.getResource(relativePath)
                    .toExternalForm();
        } catch (NullPointerException e) {
            throw new BusinessException(relativePath + "不存在");
        }
    }

    /**
     * 从classpath中读取文件
     * 
     * @param relativePath /开头从classpath读取,否则是相对于ResourceUtils.class的路径
     * @return
     */
    public static InputStream loadResource(String relativePath) {
        return ResourceUtils.class.getResourceAsStream(relativePath);
    }

    /**
     * 根据名称返回css文件路径,只从系统默认的样式文件目录(/css)查找
     * 
     * @param cssName
     * @return
     */
    public static String cssUrl(String cssName) {
        if (!cssName.endsWith(".css")) {
            cssName = cssName + ".css";
        }
        String relPath = UrlHelper.mergeUrl(CSS_DIR, cssName);
        return ResourceUtils.absUrl(relPath);
    }

    /**
     * 根据名称返回图片文件路径,只从系统默认的图片目录(/images)查找
     * 
     * @param imageName 图片名
     * @return
     */
    public static String imageUrl(String imageName) {
        String relPath = UrlHelper.mergeUrl(IMG_DIR, imageName);
        return ResourceUtils.absUrl(relPath);
    }

    public static String assetUrl(String assetName) {
        String relPath = UrlHelper.mergeUrl(ASSETS_DIR, assetName);
        return ResourceUtils.absUrl(relPath);
    }

    /**
     * 根据名称返回fxml文件路径URL,只从系统默认的fxml文件目录(/org/jflame/devAide)查找
     * 
     * @param fxmlName
     * @return
     */
    public static URL fxmlUrl(final String fxmlName) {
        return App.class.getResource(fxmlName.endsWith(".fxml") ? FXML_DIR + fxmlName : FXML_DIR + fxmlName + ".fxml");
    }

    public static ResourceBundle getResourceBundle() {
        ResourceBundle resourceBundle = ResourceBundle.getBundle(BUNDLE_NAME, AppContext.getLocale());
        return resourceBundle;
    }

    public static String getString(String key) {
        return getResourceBundle().getString(key);
    }

    public static String getString(String key, Object... args) {
        String fmt = getString(key);
        if (fmt != null) {
            return MessageFormat.format(fmt, args);
        } else {
            return key;
        }
    }

    public static String getSvgContent(String name) {
        return svgProperties.getProperty(name);
    }

}
